package view;

import services.Service;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableColumnModel;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

public class CustomerManagerJPanel implements ActionListener, MouseListener, DocumentListener {
    // 定义全局组件
    JPanel backgroundPanel, topPanel, toolPanel, searchPanel, tablePanel;
    JTextField inputName;
    BaseTableModule baseTableModule;
    JTable table;
    JScrollPane jScrollPane;
    JLabel toolAdd, toolModify, toolDelete;;

    Service service;

    public CustomerManagerJPanel(Service service) {
        this.service = service;
        backgroundPanel = new JPanel(new BorderLayout());

        initTopPanel();
        initTablePanel();
    }
    // 初始化顶部面板
    private void initTopPanel() {
        topPanel = new JPanel(new BorderLayout());

        initToolPanel();
        initSearchPanel();
        backgroundPanel.add(topPanel, "North");
    }

    // 初始化工具面板
    public void initToolPanel() {
        toolPanel = new JPanel();
        // 工具图标
        Icon icon_add = new ImageIcon("image/add.png");
        toolAdd = new JLabel(icon_add);
        toolAdd.setToolTipText("新建");
        toolAdd.addMouseListener(this);

        Icon icon_modify = new ImageIcon("image/modify.png");
        toolModify = new JLabel(icon_modify);
        toolModify.setToolTipText("修改");
        toolModify.addMouseListener(this);

        Icon icon_delete = new ImageIcon("image/delete.png");
        toolDelete = new JLabel(icon_delete);
        toolDelete.setToolTipText("删除");
        toolDelete.addMouseListener(this);

        toolPanel.add(toolAdd);
        toolPanel.add(toolModify);
        toolPanel.add(toolDelete);

        topPanel.add(toolPanel, "West");
    }
    // 初始化搜素条件面板
    public void initSearchPanel() {

        searchPanel = new JPanel();

        // 商品模糊名称输入框
        inputName = new JTextField(10);
        inputName.getDocument().addDocumentListener(this);
        searchPanel.add(inputName);
        topPanel.add(searchPanel, "East");
    }
    // 初始化数据表格面板
    public void initTablePanel() {

        String[] params = { "编号", "名字", "电话", "邮箱", "地址","录入时间"};

        Vector<Vector> vector = new Vector<Vector>();
        try {
            vector = service.selectAllVexctor(2);
        } catch (Exception e) {
            e.printStackTrace();
        }

        baseTableModule = new BaseTableModule(params, vector);

        table = new JTable(baseTableModule);
        Tools.setTableStyle(table);
        DefaultTableColumnModel dcm = (DefaultTableColumnModel) table.getColumnModel();// 获取列模型
        dcm.getColumn(0).setMinWidth(0); // 将第一列的最小宽度、最大宽度都设置为0
        dcm.getColumn(0).setMaxWidth(0);

        jScrollPane = new JScrollPane(table);
        Tools.setJspStyle(jScrollPane);

        tablePanel = new JPanel(new BorderLayout());
        tablePanel.setOpaque(false);

        tablePanel.add(jScrollPane);

        backgroundPanel.add(tablePanel, "Center");
    }

    // 更新数据表格
    public void refreshTablePanel() {
        backgroundPanel.remove(tablePanel);
        String nameString = inputName.getText();

        String[] params = { "编号", "名字", "电话", "邮箱", "地址","录入时间"};

        Vector<Vector> vector = new Vector<Vector>();
        try {
            vector = service.selectByName(2,nameString);
        } catch (Exception e) {
            e.printStackTrace();
        }

        baseTableModule = new BaseTableModule(params, vector);

        table = new JTable(baseTableModule);
        Tools.setTableStyle(table);
        DefaultTableColumnModel dcm = (DefaultTableColumnModel) table.getColumnModel();// 获取列模型
        dcm.getColumn(0).setMinWidth(0); // 将第一列的最小宽度、最大宽度都设置为0
        dcm.getColumn(0).setMaxWidth(0);

        jScrollPane = new JScrollPane(table);
        Tools.setJspStyle(jScrollPane);

        tablePanel = new JPanel(new BorderLayout());
        tablePanel.setOpaque(false);

        tablePanel.add(jScrollPane);

        backgroundPanel.add(tablePanel, "Center");

        backgroundPanel.validate();
    }
    @Override
    public void actionPerformed(ActionEvent e) {

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == toolAdd) {
            new AddCustomerJFrame(service, this);
        } else if (e.getSource() == toolModify) {
            int row = table.getSelectedRow();
            if (row < 0) {
                JOptionPane.showMessageDialog(null, "请选择客户");
            } else {
                new ModifyCustomerJFrame(service,this, table, row);
            }

        } else if (e.getSource() == toolDelete) {
            int row = table.getSelectedRow();
            if (row < 0) {
                JOptionPane.showMessageDialog(null, "请选择客户");
            } else {
                String id = (String) table.getValueAt(row, 0);
                int result = JOptionPane.showConfirmDialog(null, "是否确定删除？", "用户提示", JOptionPane.YES_NO_OPTION);
                if (result == 0) {
                    try {
                        int tempresult = service.deleteById(2,id);
                        if (tempresult > 0) {
                            JOptionPane.showMessageDialog(null, "删除成功！");
                            refreshTablePanel();
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        if (e.getSource() == toolAdd) {
            toolAdd.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        } else if (e.getSource() == toolModify) {
            toolModify.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        } else if (e.getSource() == toolDelete) {
            toolDelete.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        refreshTablePanel();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        refreshTablePanel();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {

    }

}
